package com.bnq.logc.db.tmp

import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import androidx.core.database.getLongOrNull
import androidx.core.database.getStringOrNull
import com.bnq.logc.data.FileInfoBean
import com.bnq.logc.db.LogcDatabase

object LogcTmpDao {

    private val mDatabase = LogcDatabase()

    private var TABLE_FILE_INFO = "tmp_file_info"

    //val fileName:String,var fileLastUpdateSize:Long,var fileLastUpdateDigest:String?,var modifyTime:Long
    private val sql_tmp_file_info = "create table if not exists tmp_file_info (_id integer primary key autoincrement, file_name varchar(200), path varchar(1000), file_last_update_size integer, file_last_update_digest varchar(128), create_time integer, modify_time integer)"

    fun init(context: Context){
        mDatabase.init(context,"logc_tmp_file_db")
        mDatabase.createTable(sql_tmp_file_info)
    }

    private fun contentValuesByObj(bean: FileInfoBean):ContentValues{
        val values = ContentValues()
        //values.put("_id",bean._id)
        values.put("file_name",bean.fileName)
        values.put("path",bean.path)
        values.put("file_last_update_size",bean.fileLastUpdateSize)
        values.put("file_last_update_digest",bean.fileLastUpdateDigest)
        values.put("create_time",bean.createTime)
        values.put("modify_time",bean.modifyTime)
        return values
    }

    fun inset(bean: FileInfoBean):Long{
        val values = contentValuesByObj(bean)
        return mDatabase.insert(TABLE_FILE_INFO,values)
    }

    fun update(bean: FileInfoBean):Int{
        val values = contentValuesByObj(bean)
        return if(bean._id > 0){
            mDatabase.update(TABLE_FILE_INFO,values,"_id = ?", arrayOf("${bean._id}"))
        }else if(bean.path.isNotBlank()){
            mDatabase.update(TABLE_FILE_INFO,values,"path = ?", arrayOf(bean.path))
        }else{
            0
        }
    }

    fun delete(bean: FileInfoBean):Int{
        return if(bean._id > 0){
            mDatabase.delete(TABLE_FILE_INFO,"_id = ?", arrayOf("${bean._id}"))
        }else if(bean.path.isNotBlank()){
            mDatabase.delete(TABLE_FILE_INFO,"path = ?", arrayOf(bean.path))
        }else{
            0
        }
    }

    fun queryAll():List<FileInfoBean>{
        val cursor = mDatabase.query(TABLE_FILE_INFO, true, null, null, null, null, null, null, null)
        return cursorToList(cursor)
    }

    fun querySingle(_id:Long):FileInfoBean?{
        val cursor = mDatabase.query(TABLE_FILE_INFO, true, null, "_id = ?", arrayOf("$_id"), null, null, null, null)
        return cursorToList(cursor).firstOrNull()
    }

    fun querySingle(fileName:String):FileInfoBean?{
        val cursor = mDatabase.query(TABLE_FILE_INFO, true, null, "file_name = ?", arrayOf(fileName), null, null, null, null)
        return cursorToList(cursor).firstOrNull()
    }

    fun querySingleByPath(path:String):FileInfoBean?{
        val cursor = mDatabase.query(TABLE_FILE_INFO, true, null, "path = ?", arrayOf(path), null, null, null, null)
        return cursorToList(cursor).firstOrNull()
    }

    fun queryList(limit:Int):List<FileInfoBean>{
        val cursor = mDatabase.query(TABLE_FILE_INFO, true, null, null, null, null, null, "_id DESC", "$limit")
        return cursorToList(cursor)
    }

    private fun cursorToList(cursor: Cursor):List<FileInfoBean>{
        val result = mutableListOf<FileInfoBean>()
        if(cursor.moveToFirst()){
            do {
                val _idColumnIndex = cursor.getColumnIndex("_id")
                val _id = cursor.getLongOrNull(_idColumnIndex) ?: continue
                val fileNameColumnIndex = cursor.getColumnIndex("file_name")
                val fileName = cursor.getStringOrNull(fileNameColumnIndex)?:""
                val pathColumnIndex = cursor.getColumnIndex("path")
                val path = cursor.getStringOrNull(pathColumnIndex)?:""
                val fileSizeColumnIndex = cursor.getColumnIndex("file_last_update_size")
                val fileSize = cursor.getLongOrNull(fileSizeColumnIndex)?:0
                val fileDigestColumnIndex = cursor.getColumnIndex("file_last_update_digest")
                val fileDigest = cursor.getStringOrNull(fileDigestColumnIndex)?:""
                val createTimeColumnIndex = cursor.getColumnIndex("create_time")
                val createTime = cursor.getLongOrNull(createTimeColumnIndex)?:0
                val modifyTimeColumnIndex = cursor.getColumnIndex("modify_time")
                val modifyTime = cursor.getLongOrNull(modifyTimeColumnIndex)?:0
                result.add(FileInfoBean(_id,fileName,path,fileSize,fileDigest,createTime,modifyTime))
            }while (cursor.moveToNext())
        }
        cursor.close()
        return result
    }
}